package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜品相关接口
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private DishFlavorService dishFlavorService;

    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类Id查询菜品")
    public Result list(Long categoryId){
        List<Dish> dish = dishService.listId(categoryId);
        return Result.success(dish);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){

        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("菜品起售停售")
    public Result updateStatus(@PathVariable Integer status,Long id){
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishService.updateStatus(dish);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){

        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findIdById(@PathVariable Long id){
      DishVO dishVO =  dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @Transactional(rollbackFor = Exception.class)
    public Result add(@RequestBody DishDTO dishDTO){
        Dish dish = new Dish();

        //添加菜品
        BeanUtils.copyProperties(dishDTO,dish);
        dishService.add(dish);

        //健壮性优化：苍穹业务要求：菜品必须有口味，没有口味数据用户端会报错
        if(dishDTO.getFlavors()==null || dishDTO.getFlavors().size()==0 ){
            throw  new BaseException("口味数据不能为空行");
        }

        //2.新增多条口味数据写入数据库
        //List<DishFlavor> dishFlavor = new ArrayList<>();
        //BeanUtils.copyProperties(dishDTO.getFlavors(),dishFlavor);
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            flavor.setDishId(dish.getId());
        }
        //添加口味
        //dishFlavor.forEach(dishFlavor1 -> dishFlavor1.setDishId(dish.getId()));
        dishFlavorService.adddishFlavor(dishDTO.getFlavors());


        return Result.success();
    }
}
